package com.gopay.common.user.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.constants.cust.CustCorpInfoConstants;
import com.gopay.common.domain.cust.CustCorpInfo;
import com.gopay.common.domain.cust.CustPersonInfo;
import com.gopay.common.user.dao.CustCorpInfoQueryDAO;

/**
 * CustCorpInfoQuery DAO接口实现
 *
 * @CustCorpInfoQueryDAOImpl.java
 * @author liuxin
 * @2013-1-5 上午9:43:39 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("custCorpInfoQueryDao")
public class CustCorpInfoQueryDAOImpl extends BaseDAOImpl<CustCorpInfo, Serializable> implements CustCorpInfoQueryDAO {


    @Override
    public CustCorpInfo findCustEmail(String email) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("email",email).ignoreCase());
        criteria.add(Restrictions.ne("corpStat",CustCorpInfoConstants.CORP_STAT_WAIT_EMAIL_CERTIFY));
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

    /* (non-Javadoc)
     * @see com.gopay.common.user.dao.CustCorpInfoQueryDAO#findCustCorpOpen(java.lang.String)
     */
    @Override
    public List<CustCorpInfo> findCustCorpOpen(String email) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        String stat [] = {CustCorpInfoConstants.CORP_STAT_NORMAL,CustCorpInfoConstants.CORP_STAT_CONSOLE_LOCK};
        criteria.add(Restrictions.eq("email",email));
        criteria.add(Restrictions.in("corpStat",stat));
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list;
    }

    @Override
    public CustCorpInfo findByCorpName(String corpName) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("corpName",corpName));
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

    @Override
    public CustCorpInfo findByMerName(String merName) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("merName",merName));
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

    @Override
    public List<CustCorpInfo> findAllByCorpName(String corpName) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("corpName",corpName));
        criteria.add(Restrictions.eq("isRealNameCertify","1"));
        criteria.add(Restrictions.eq("corpStat", CustCorpInfoConstants.CORP_STAT_NORMAL));
        criteria.addOrder(Order.desc("createdTs"));//排序
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list;
    }

	@Override
	public CustCorpInfo findCustMobile(String mobile) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
       // criteria.add(Restrictions.eq("custMob",mobile));

        /**start 央行检测敏舒数据   ssj 2017-05-04  加密  考虑到后期可能会有多个秘钥体系采用in进行查询*/
        criteria.add(Restrictions.in("custMob",SensitiveInfoUtils.getAllHisCipherText(mobile).toArray()));
        /**end*/

        criteria.add(Restrictions.ne("corpStat",CustCorpInfoConstants.CORP_STAT_WAIT_EMAIL_CERTIFY));
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
	}

	@Override
	public int findByCertTypeAndCertNo(String certType, String certNo) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("legalPersonCertType",certType));
        /**
         * modified by zyt 2017-5-5 身份证号已加密，查询使用in
         */
        criteria.add(Restrictions.in("legalPersonCertNo",SensitiveInfoUtils.getAllHisCipherText(certNo).toArray()));
        //criteria.add(Restrictions.eq("legalPersonCertNo",certNo));
        return this.findCountByCriteria(criteria);
	}

    @Override
    public CustCorpInfo getNoActiveCustByCustEmail(String email) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("email",email).ignoreCase());
        criteria.add(Restrictions.eq("corpStat", CustCorpInfoConstants.CORP_STAT_WAIT_EMAIL_CERTIFY));
        criteria.addOrder(Order.desc("createdTs"));
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

    @Override
    public CustCorpInfo getNoActiveCustByCustMob(String mobile) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
//        criteria.add(Restrictions.eq("custMob",mobile));

        /**start 央行检测敏舒数据   ssj 2017-05-04  加密  考虑到后期可能会有多个秘钥体系采用in进行查询*/
        criteria.add(Restrictions.in("custMob",SensitiveInfoUtils.getAllHisCipherText(mobile).toArray()));
        /**end*/

        criteria.add(Restrictions.eq("corpStat",CustCorpInfoConstants.CORP_STAT_WAIT_EMAIL_CERTIFY));
        criteria.addOrder(Order.desc("createdTs"));
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }
    @Override
    public CustCorpInfo findByMobileNo(String mobileNo) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
//        criteria.add(Restrictions.eq("custMob",mobileNo));

        /**start 央行检测敏舒数据   ssj 2017-05-04  加密  考虑到后期可能会有多个秘钥体系采用in进行查询*/
        criteria.add(Restrictions.in("custMob",SensitiveInfoUtils.getAllHisCipherText(mobileNo).toArray()));
        /**end*/

        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

	@Override
	public CustCorpInfo getByCustId(String custId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("custId",custId));
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
	}
    @Override
	public List<CustCorpInfo> getByEmailAndCustStat(String email,String[] custStat) {
    	DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("email",email));
        criteria.add(Restrictions.in("corpStat",custStat));
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list;
    }

	@Override
    public CustCorpInfo findByEmail(String email, String custName, String certType) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("email",email).ignoreCase());
        criteria.add(Restrictions.ne("corpStat",CustCorpInfoConstants.CORP_STAT_WAIT_EMAIL_CERTIFY));
        if (custName != null && "".equals(custName)){
            criteria.add(Restrictions.eq("corpName",custName).ignoreCase());
        }
        if (certType != null && "".equals(certType)){
            criteria.add(Restrictions.eq("certType",certType).ignoreCase());
        }
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

    @Override
    public CustCorpInfo findByMobile(String mob, String custName, String certType) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
//        criteria.add(Restrictions.eq("custMob",mob));

        /**start 央行检测敏舒数据   ssj 2017-05-04  加密  考虑到后期可能会有多个秘钥体系采用in进行查询*/
        criteria.add(Restrictions.in("custMob",SensitiveInfoUtils.getAllHisCipherText(mob).toArray()));
        /**end*/
        if (custName != null && "".equals(custName)){
            criteria.add(Restrictions.eq("corpName",custName).ignoreCase());
        }
        if (certType != null && "".equals(certType)){
            criteria.add(Restrictions.eq("certType",certType).ignoreCase());
        }
        List<CustCorpInfo> list = this.findByCriteria(criteria);
        return list.size()>0 ? list.get(0): null;
    }

    @Override
    public List<CustCorpInfo> getByEmailWithoutStat(String email) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("email",email));
        return this.findByCriteria(criteria);

    }

    @Override
    public List<CustCorpInfo> getInfoByCustIds(List<Object> custIds) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.in("custId", custIds));
        return this.findByCriteria(criteria);
    }
    
    @Override
    public List<CustCorpInfo> findByMerCustId(String merId,String MerCustId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(CustCorpInfo.class);
        criteria.add(Restrictions.eq("merCustId",merId+"_"+MerCustId));
        
        return this.findByCriteria(criteria);
    }
}
